/*
 * @(#) LogicException.java 2010/04/26 
 * 
 * Copyright (c) 2010, Strong Inc. All Rights Reserved.
 * Strong Inc. CONFIDENTIAL
 */
package cn.strong.gweva.common.execption;

import java.util.ArrayList;
import java.util.List;

/**
 * 业务异常
 * 
 * @author lilanfei
 * @version 1.0
 * @since 2010/04/26  
 */
public class ServiceException extends RuntimeException {
	
	/** <code>serialVersionUID</code> */
	private static final long serialVersionUID = 6606740531625486433L;
	
	/** 消息ID */
	private String key;
	/** 消息参数 */
	private List<Object> replacements = new ArrayList<Object>();

	private int index = -1;

	private boolean find = true;
	
	private String value;

	/**
	 * 取得消息ID。
	 * 
	 * @return 消息ＩＤ
	 */
	public String getKey() {
		return this.key;
	}

	/**
	 * 取得消息参数。
	 * 
	 * @return 消息参数
	 */
	public Object[] getReplacements() {
		return this.replacements.toArray(new Object[this.replacements.size()]);
	}

	/**
	 * @return
	 */
	public int getIndex() {
		return this.index;
	}

	/**
	 * @param index
	 */
	public void setIndex(int index) {
		this.index = index;
	}

	/**
	 * @return
	 */
	public boolean hasIndex() {
		return this.index != -1;
	}

	/**
	 * @return
	 */
	public boolean isFind() {
		return this.find;
	}

	/**
	 * @return
	 */
	public String getValue() {
		return this.value;
	}

	/**
	 * 业务异常构造函数
	 * 
	 * @param key 消息ＩＤ
	 */
	public ServiceException(String key) {
		super(key);
		this.key = key;
	}

	/**
	 * 业务异常构造函数
	 * 
	 * @param key 消息ID
	 * @param replacement1 消息参数1
	 */
	public ServiceException(String key, Object replacement1) {
		this(key);
		addReplacement(replacement1);
	}

	/**
	 * 业务异常构造函数
	 * 
	 * @param key 消息ID
	 * @param replacement1 消息参数1
	 * @param replacement2 消息参数2
	 */
	public ServiceException(String key, Object replacement1, Object replacement2) {
		this(key);
		addReplacement(replacement1);
		addReplacement(replacement2);
	}

	/**
	 * 业务异常构造函数
	 * 
	 * @param key 消息ID
	 * @param replacement1 消息参数1
	 * @param replacement2 消息参数2
	 * @param replacement3 消息参数3
	 */
	public ServiceException(String key, Object replacement1, Object replacement2,
			Object replacement3) {
		this(key);
		addReplacement(replacement1);
		addReplacement(replacement2);
		addReplacement(replacement3);
	}

	/**
	 * 业务异常构造函数
	 * 
	 * @param key 消息ID
	 * @param replacement1 消息参数1
	 * @param replacement2 消息参数2
	 * @param replacement3 消息参数3
	 * @param replacement4 消息参数4
	 */
	public ServiceException(String key, Object replacement1, Object replacement2,
			Object replacement3, Object replacement4) {
		this(key);
		addReplacement(replacement1);
		addReplacement(replacement2);
		addReplacement(replacement3);
		addReplacement(replacement4);
	}

	/**
	 * 业务异常构造函数
	 * 
	 * @param key 消息ID
	 * @param replacementArray 消息参数数组
	 */
	public ServiceException(String key, Object[] replacementArray) {
		this(key);
		addReplacements(replacementArray);
	}

	/**
	 * 业务异常构造函数
	 * 
	 * @param key 消息ID
	 * @param cause
	 */
	public ServiceException(String key, Throwable cause) {
		super(key, cause);
		this.key = key;
	}

	/**
	 * 业务异常构造函数
	 * 
	 * @param key 消息ID
	 * @param replacement1 消息参数1
	 * @param cause
	 */
	public ServiceException(String key, Object replacement1, Throwable cause) {
		this(key, cause);
		addReplacement(replacement1);
	}

	/**
	 * 业务异常构造函数
	 * 
	 * @param key 消息ID
	 * @param replacement1  消息参数1
	 * @param replacement2  消息参数2
	 * @param cause
	 */
	public ServiceException(String key, Object replacement1, Object replacement2,
			Throwable cause) {
		this(key, cause);
		addReplacement(replacement1);
		addReplacement(replacement2);
	}

	/**
	 * 业务异常构造函数
	 * 
	 * @param key 消息ID
	 * @param replacement1  消息参数1
	 * @param replacement2  消息参数2
	 * @param replacement3  消息参数3
	 * @param cause
	 */
	public ServiceException(String key, Object replacement1, Object replacement2,
			Object replacement3, Throwable cause) {
		this(key, cause);
		addReplacement(replacement1);
		addReplacement(replacement2);
		addReplacement(replacement3);
	}

	/**
	 * 业务异常构造函数
	 * 
	 * @param key 消息ID
	 * @param replacement1  消息参数1
	 * @param replacement2  消息参数2
	 * @param replacement3  消息参数3
	 * @param replacement4  消息参数4
	 * @param cause
	 */
	public ServiceException(String key, Object replacement1, Object replacement2,
			Object replacement3, Object replacement4, Throwable cause) {
		this(key, cause);
		addReplacement(replacement1);
		addReplacement(replacement2);
		addReplacement(replacement3);
		addReplacement(replacement4);
	}

	/**
	 * 业务异常构造函数
	 * 
	 * @param key 消息ID
	 * @param replacementArray 消息参数数组
	 * @param cause
	 */
	public ServiceException(String key, Object[] replacementArray, Throwable cause) {
		this(key, cause);
		addReplacements(replacementArray);
	}

	/**
	 * 业务异常构造函数
	 * 
	 * @param key 消息ID
	 * @param index
	 */
	public ServiceException(String key, int index) {
		super(key);
		this.key = key;
		this.index = index;
	}

	/**
	 * 业务异常构造函数
	 * 
	 * @param key 消息ID
	 * @param replacement1  消息参数1
	 * @param index
	 */
	public ServiceException(String key, Object replacement1, int index) {
		this(key, index);
		addReplacement(replacement1);
	}

	/**
	 * 业务异常构造函数
	 * 
	 * @param key 消息ID
	 * @param replacement1  消息参数1
	 * @param replacement2  消息参数2
	 * @param index
	 */
	public ServiceException(String key, Object replacement1, Object replacement2,
			int index) {
		this(key, index);
		addReplacement(replacement1);
		addReplacement(replacement2);
	}

	/**
	 * 业务异常构造函数
	 * 
	 * @param key 消息ID
	 * @param replacement1  消息参数1
	 * @param replacement2  消息参数2
	 * @param replacement3  消息参数3
	 * @param index
	 */
	public ServiceException(String key, Object replacement1, Object replacement2,
			Object replacement3, int index) {
		this(key, index);
		addReplacement(replacement1);
		addReplacement(replacement2);
		addReplacement(replacement3);
	}

	/**
	 * 业务异常构造函数
	 * 
	 * @param key 消息ID
	 * @param replacement1  消息参数1
	 * @param replacement2  消息参数2
	 * @param replacement3  消息参数3
	 * @param replacement4  消息参数4
	 * @param index
	 */
	public ServiceException(String key, Object replacement1, Object replacement2,
			Object replacement3, Object replacement4, int index) {
		this(key, index);
		addReplacement(replacement1);
		addReplacement(replacement2);
		addReplacement(replacement3);
		addReplacement(replacement4);
	}

	/**
	 * 业务异常构造函数
	 * 
	 * @param key 消息ID
	 * @param replacementArray 消息参数数组
	 * @param index
	 */
	public ServiceException(String key, Object[] replacementArray, int index) {
		this(key, index);
		addReplacements(replacementArray);
	}

	/**
	 * 业务异常构造函数
	 * 
	 * @param key 消息ID
	 * @param index
	 * @param cause
	 */
	public ServiceException(String key, int index, Throwable cause) {
		super(key, cause);
		this.key = key;
		this.index = index;
	}

	/**
	 * 业务异常构造函数
	 * 
	 * @param key 消息ID
	 * @param replacement1  消息参数1
	 * @param index
	 * @param cause
	 */
	public ServiceException(String key, Object replacement1, int index,
			Throwable cause) {
		this(key, index, cause);
		addReplacement(replacement1);
	}

	/**
	 * 业务异常构造函数
	 * 
	 * @param key 消息ID
	 * @param replacement1  消息参数1
	 * @param replacement2  消息参数2
	 * @param index
	 * @param cause
	 */
	public ServiceException(String key, Object replacement1, Object replacement2,
			int index, Throwable cause) {
		this(key, index, cause);
		addReplacement(replacement1);
		addReplacement(replacement2);
	}

	/**
	 * 业务异常构造函数
	 * 
	 * @param key 消息ID
	 * @param replacement1  消息参数1
	 * @param replacement2  消息参数2
	 * @param replacement3  消息参数3
	 * @param index
	 * @param cause
	 */
	public ServiceException(String key, Object replacement1, Object replacement2,
			Object replacement3, int index, Throwable cause) {
		this(key, index, cause);
		addReplacement(replacement1);
		addReplacement(replacement2);
		addReplacement(replacement3);
	}

	/**
	 * 业务异常构造函数
	 * 
	 * @param key 消息ID
	 * @param replacement1  消息参数1
	 * @param replacement2  消息参数2
	 * @param replacement3  消息参数3
	 * @param replacement4  消息参数4
	 * @param index
	 * @param cause
	 */
	public ServiceException(String key, Object replacement1, Object replacement2,
			Object replacement3, Object replacement4, int index, Throwable cause) {
		this(key, index, cause);
		addReplacement(replacement1);
		addReplacement(replacement2);
		addReplacement(replacement3);
		addReplacement(replacement4);
	}

	/**
	 * 业务异常构造函数
	 * 
	 * @param key 消息ID
	 * @param replacementArray 消息参数数组
	 * @param index
	 * @param cause
	 */
	public ServiceException(String key, Object[] replacementArray, int index,
			Throwable cause) {
		this(key, index, cause);
		addReplacements(replacementArray);
	}

	/**
	 * 业务异常构造函数
	 * 
	 * @param key 消息ID
	 * @param message 消息
	 * @param find
	 */
	public ServiceException(String key, String message, boolean find) {
		super(key);

		this.find = find;
		this.key = key;
		this.value = message;
	}

	/**
	 * 业务异常构造函数
	 * 
	 * @param key 消息ID
	 * @param message 消息
	 * @param find
	 * @param cause
	 */
	public ServiceException(String key, String message, boolean find,
			Throwable cause) {
		super(key, cause);

		this.find = find;
		this.key = key;
		this.value = message;
	}

	/**
	 * 添加消息参数
	 * 
	 * @param replacement 消息参数
	 */
	public void addReplacement(Object replacement) {
		this.replacements.add(replacement);
	}

	/**
	 * 添加消息参数
	 * 
	 * @param replacementArray 消息参数数组
	 */
	public void addReplacements(Object[] replacementArray) {
		for (int i = 0; i < replacementArray.length; ++i)
			this.replacements.add(replacementArray[i]);
	}
}
